package com.zx.auth.block.param;

import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.enums.DateTimeFormatEnum;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.Update;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * <p>
 * description: OAuth2客户端参数类 <br>
 * create: 2025-11-12 17:00
 * </p>
 *
 * @author
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class OAuth2ClientParam implements Serializable, Create, Update, Query {
    /**
     * 主键ID，由应用层生成
     */
    @ParameterValidation(index = 0, desc = "客户端ID", rules = {ValidateRulesEnum.NULL_LENGTH}, length = "0~100")
    private String id;

    /**
     * 客户端ID，用于OAuth2流程中识别客户端
     */
    @ParameterValidation(index = 0, desc = "客户端标识", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~100")
    private String clientId;

    /**
     * 客户端ID发放时间
     */
    @ParameterValidation(index = 0, desc = "客户端ID发放时间", rules = {ValidateRulesEnum.NULL_DATE}, dateFormat = DateTimeFormatEnum.YEAR)
    private LocalDateTime clientIdIssuedAt;

    /**
     * 客户端密钥，可能为空(如PKCE流程)
     */
    @ParameterValidation(index = 0, desc = "客户端密钥", rules = {ValidateRulesEnum.NULL_LENGTH}, length = "0~200")
    private String clientSecret;

    /**
     * 客户端密钥过期时间，null表示永不过期
     */
    @ParameterValidation(index = 0, desc = "客户端密钥过期时间", rules = {ValidateRulesEnum.NULL_DATE}, dateFormat = DateTimeFormatEnum.YEAR)
    private LocalDateTime clientSecretExpiresAt;

    /**
     * 客户端名称，显示给用户看的名称
     */
    @ParameterValidation(index = 0, desc = "客户端名称", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~200")
    private String clientName;

    /**
     * 客户端认证方法(client_secret_basic等)，多个方法用逗号分隔
     */
    @ParameterValidation(index = 0, desc = "客户端认证方法", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~1000")
    private String clientAuthenticationMethods;

    /**
     * 支持的授权类型(authorization_code等)，多个类型用逗号分隔
     */
    @ParameterValidation(index = 0, desc = "授权类型", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~1000")
    private String authorizationGrantTypes;

    /**
     * 重定向URI列表，多个URI用逗号分隔
     */
    @ParameterValidation(index = 0, desc = "重定向URI", rules = {ValidateRulesEnum.NULL_LENGTH}, length = "0~1000")
    private String redirectUris;

    /**
     * 登出后重定向URI列表，多个URI用逗号分隔
     */
    @ParameterValidation(index = 0, desc = "登出重定向URI", rules = {ValidateRulesEnum.NULL_LENGTH}, length = "0~1000")
    private String postLogoutRedirectUris;

    /**
     * 支持的scope范围，多个scope用逗号分隔
     */
    @ParameterValidation(index = 0, desc = "权限范围", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~1000")
    private String scopes;

    /**
     * 客户端设置(JSON格式)，包含客户端特定的配置
     */
    @ParameterValidation(index = 0, desc = "客户端设置", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~2000")
    private String clientSettings;

    /**
     * 令牌设置(JSON格式)，包含令牌相关的配置
     */
    @ParameterValidation(index = 0, desc = "令牌设置", rules = {ValidateRulesEnum.NOT_NULL, ValidateRulesEnum.LENGTH}, length = "1~2000")
    private String tokenSettings;

    /**
     * 当前页码
     */
    @ParameterValidation(index = 0, desc = "当前页码", rules = {ValidateRulesEnum.NUMBER_MIN}, numberMin = 1)
    private Long pageNum = 1L;

    /**
     * 每页条数
     */
    @ParameterValidation(index = 0, desc = "每页条数", rules = {ValidateRulesEnum.NUMBER_MIN, ValidateRulesEnum.NUMBER_MAX}, numberMin = 1, numberMax = 1000)
    private Long pageSize = 10L;

    @Override
    public Set<String> getCreateGroupFields() {
        return Set.of("clientId", "clientIdIssuedAt", "clientSecret", "clientSecretExpiresAt",
                "clientName", "clientAuthenticationMethods", "authorizationGrantTypes",
                "redirectUris", "postLogoutRedirectUris", "scopes", "clientSettings", "tokenSettings");
    }

    @Override
    public Set<String> getUpdateGroupFields() {
        return Set.of("id", "clientName", "clientSecret", "clientSecretExpiresAt",
                "clientAuthenticationMethods", "authorizationGrantTypes",
                "redirectUris", "postLogoutRedirectUris", "scopes", "clientSettings", "tokenSettings");
    }

    @Override
    public Set<String> getQueryGroupFields() {
        return Set.of("id", "clientId", "clientName", "pageNum", "pageSize");
    }
}